//
//  NetworkTarget.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/19.
//  Copyright © 2019 Hins. All rights reserved.
//

import Foundation
import Moya


enum NetworkTarget {
    case category
    case feeds(_ req: FeedsEntityRequest)
    case login(_ req: LoginRequest)
    case registerValidate(_ req: RegisterValidateRequest)
    case register(_ req: RegisterRequest)
    case userNameChange(_ req: UserNameChangeRequest)
    case userAvatarChange(_ req: UserAvatarChangeRequest)
    case liveStartRequest(_ req: LiveStartRequest)
}

extension NetworkTarget: TargetType {
    var baseURL: URL { return URL(string: Network.host)! }
    
    var path: String {
        switch self {
        case .category:
            return "/feeds/category/"
        case .feeds:
            return "/feeds/page/"
        case .login:
            return "/user/login/"
        case .registerValidate:
            return "/user/registerValidate/"
        case .register:
            return "/user/register/"
        case .userNameChange:
            return "/user/changeUserName/"
        case .userAvatarChange:
            return "/user/changeUserAvatar/"
        case .liveStartRequest:
            return "/live/livestart/"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .category:
            return .get
        default:
            return .post
        }
    }
    
    var task: Task {
        switch self {
        case .category:
            return .requestPlain
        case .feeds(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .login(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .registerValidate(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .register(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .userNameChange(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .userAvatarChange(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        case .liveStartRequest(let req):
            guard let data = try? req.serializedData() else { fatalError() }
            return .requestData(data)
        }
    }
    
    var sampleData: Data {
        #if DEBUG
        switch self {
        case .category:
            return mock_category()
        case .feeds(let req):
            return mock_feeds(req)
        case .login(let req):
            return mock_login(req)
        case .registerValidate(let req):
            return mock_registerValidate(req)
        case .register(let req):
            return mock_register(req)
        case .userNameChange(let req):
            return mock_changeName(req)
        case .userAvatarChange(let req):
            return mock_changeAvatar(req)
        case .liveStartRequest(let req):
            return mock_LiveStartRequest(req)
        }
        #else
        return Data()
        #endif
    }
    
    var headers: [String : String]? {
        return nil
    }
}

#if DEBUG

// MARK: Mock Data
private extension NetworkTarget {
    func mock_category() -> Data {
        let titles = ["学习", "游戏", "户外", "体育", "桌游", "才艺", "颜值", "美食", "综合"]
        let model = FeedsCategory.with {
            $0.categories = titles.enumerated().map { (index, title) -> FeedsCategory.CategoryItem in
                return FeedsCategory.CategoryItem.with { item in
                    item.title = title
                    item.fid = String(index)
                }
            }
        }
        return try! PBResponse.with {
            $0.result = true
            $0.data = try model.serializedData()
        }.serializedData()
    }
    
    func mock_feeds(_ req: FeedsEntityRequest) -> Data {
        let ssUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555692792431&di=6883554a8f5a307882f6c3bde6c7b45b&imgtype=0&src=http%3A%2F%2Fsinastorage.com%2Fstorage.caitou.sina.com.cn%2Fproducts%2F201612%2F08c37ddca9b8cafefa9733b898bb33ef.jpeg"
        
        let avatarUrl = "http://tupian.qqjay.com/tou2/2018/0826/2670d6307e76f8c9458783c2f59f3083.jpg"
        
        let entity = FeedsEntity.with {
            $0.feeds = (0..<10).map { _ -> FeedItem in
                FeedItem.with {
                    $0.snapshotURL = ssUrl
                    $0.avatarURL = avatarUrl
                    $0.name = "蔡徐坤"
                    $0.roomTitle = "鸡你太美！！鸡你太美！！"
                }
            }
            $0.fid = req.fid
            $0.currentPageIndex = req.page
            $0.totalPageCount = 5
        }
        
        return try! PBResponse.with {
            $0.result = true
            $0.data = try entity.serializedData()
        }.serializedData()
    }
    
    func mock_login(_ req: LoginRequest) -> Data {
        let match = req.account == "hins" && req.password == ("HEADSALT666" + "123" + "FOOTSALT999").md5
        
        let user = UserInfo.with {
            $0.account = "hins"
            $0.uid = "Ps15inA282"
            $0.name = "么么哒"
            $0.avatarURL = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3360653845,3076809436&fm=26&gp=0.jpg"
        }
        
        return try! PBResponse.with {
            $0.result = match
            if match {
                $0.data = try user.serializedData()
            } else {
                $0.errorMsg = "账号或密码错误"
            }
        }.serializedData()
    }
    
    func mock_registerValidate(_ req: RegisterValidateRequest) -> Data {
        let avaliable = req.account != "hins"
        return try! PBResponse.with {
            $0.result = avaliable
            if avaliable {
                $0.data = try PBEmpty().serializedData()
            } else {
                $0.errorMsg = "账号已被使用"
            }
        }.serializedData()
    }
    
    func mock_register(_ req: RegisterRequest) -> Data {
        let avaliable = req.account != "hins"
        
        let user = UserInfo.with {
            let uuid = "KHA15Ks3gd"
            $0.account = req.account
            $0.uid = uuid
            $0.name = "用户 " + uuid
            $0.avatarURL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555786751918&di=38516956955d35173ebb691220ac70d5&imgtype=0&src=http%3A%2F%2Fs9.rr.itc.cn%2Fr%2FwapChange%2F201412_14_6%2Fa8e2036111518547520.jpg"
        }
        
        return try! PBResponse.with {
            $0.result = avaliable
            if avaliable {
                $0.data = try user.serializedData()
            } else {
                $0.errorMsg = "账号已被使用"
            }
        }.serializedData()
    }
    
    func mock_changeName(_ req: UserNameChangeRequest) -> Data {
        let match =  "Ps15inA282" == req.uid
        let user = UserInfo.with {
            $0.account = "hins"
            $0.uid = "Ps15inA282"
            $0.name = req.name
            $0.avatarURL = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3360653845,3076809436&fm=26&gp=0.jpg"
        }
        
        return try! PBResponse.with {
            $0.result = match
            if match {
                $0.data = try user.serializedData()
            } else {
                $0.errorMsg = "请用hins哥测试账号来测试!"
            }
        }.serializedData()
    }
    
    func mock_changeAvatar(_ req: UserAvatarChangeRequest) -> Data {
        let match =  "Ps15inA282" == req.uid
        let hasData = req.imageData.count > 0
        let user = UserInfo.with {
            $0.account = "hins"
            $0.uid = "Ps15inA282"
            $0.name = "么么哒"
            $0.avatarURL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555937083364&di=e6253597b936493fd116261cf4038830&imgtype=0&src=http%3A%2F%2Fcpc.people.com.cn%2FNMediaFile%2F2014%2F0702%2FMAIN201407021616241952107716676.jpg"
        }
        
        return try! PBResponse.with {
            $0.result = match && hasData
            if !match {
                $0.errorMsg = "请用hins哥测试账号来测试!"
            } else if !hasData {
                $0.errorMsg = "没有图片数据"
            } else {
                $0.data = try user.serializedData()
            }
        }.serializedData()
    }
    
    func mock_LiveStartRequest(_ req: LiveStartRequest) -> Data {
        return try! PBResponse.with {
            $0.result = req.imageData.count > 0
            $0.data = try PBEmpty().serializedData()
            $0.errorMsg = "木有封面图数据"
        }.serializedData()
    }
}

#endif
